package com.sac.approval.controller;

import com.github.pagehelper.PageInfo;
import com.sac.approval.domain.LineDO;
import com.sac.approval.service.LineService;
import com.sac.common.annotation.Log;
import com.sac.common.utils.R;
import com.sac.common.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 审批路径管理
 * @author xiaodong
 * @date 2018年9月17日 上午10:54:04
 * @desc save lines
 */
@RestController
@RequestMapping("/approval/line")
public class LineController {

    @Autowired
    private LineService lineService;


    /**
     * 根据line name 查询line
     * @param lineName
     * @return
     */
    @GetMapping("/listLine/{lineName}")
    public List<LineDO> listLine(@PathVariable String lineName) {
        Map<String, Object> params = new HashMap<>();
        params.put("isDelete",'N');
        params.put("lineName",lineName);
        List<LineDO> lineList = lineService.list(params);
        return lineList;
    }

    /**
     * 查询
     * @param params
     * @return
     */
    @GetMapping("/listSavedLines")
    public PageInfo<LineDO> listSavedLines(@RequestParam Map<String, Object> params) {
        params.put("createUser", ShiroUtils.getEmpId());
        List<LineDO> savedLineList = lineService.list(params);
        PageInfo<LineDO> pageInfo = new PageInfo<>(savedLineList);
        return pageInfo;
    }


    @Log("保存line")
    @PostMapping("/save")
    R save(LineDO line) {
        line.setIsDelete("N");
        line.setCreateTime(new Date());
        line.setCreateUser(ShiroUtils.getEmpId().toString());
        if (lineService.save(line) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @Log("更新line")
    @PostMapping("/update")
    R update(LineDO line) {
        if (lineService.update(line) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @Log("批量删除saved line")
    @PostMapping("/batchRemove")
    R batchRemove(@RequestParam("ids[]") Integer[] lineIds) {
        int r = lineService.batchRemove(lineIds);
        if (r > 0) {
            return R.ok();
        }
        return R.error();
    }
}
